package io.renren.modules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.CourseContentEntity;
import io.renren.modules.admin.entity.CourseDemoEntity;
import io.renren.modules.admin.service.CourseContentService;
import io.renren.modules.admin.service.CourseDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 课程内容
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-12-17 18:24:00
 */
@RestController
@RequestMapping("admin/coursecontent")
@Api("课程内容")
public class CourseContentController {
    @Autowired
    private CourseContentService courseContentService;

    @Autowired
    private CourseDemoService courseDemoService;

    /**
     * 全部列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:coursecontent:list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = courseContentService.queryPage(params);

        return R.ok().put("page", page);
    }




    /**
     * 信息
     */
    @GetMapping("/info/{courseContentId}")
    @RequiresPermissions("admin:coursecontent:info")
    @ApiOperation("获取信息")
    public R info(@PathVariable("courseContentId") Long courseContentId){
		CourseContentEntity courseContent = courseContentService.getById(courseContentId);
        //查询课程内容
        QueryWrapper<CourseDemoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("content_id",courseContentId);
        CourseDemoEntity demoEntity = courseDemoService.getOne(wrapper);
        courseContent.setCourseDemoEntity(demoEntity);
        return R.ok().put("courseContent", courseContent);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:coursecontent:save")
    @ApiOperation("保存")
    public R save(@RequestBody CourseContentEntity courseContent){
		courseContentService.save(courseContent);
        CourseDemoEntity courseDemoEntity = courseContent.getCourseDemoEntity();
       if(courseDemoEntity!=null){
           courseDemoEntity.setContentId(courseContent.getCourseContentId());
           courseDemoService.save(courseDemoEntity);
       }

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:coursecontent:update")
    @ApiOperation("修改")
    public R update(@RequestBody CourseContentEntity courseContent){
		courseContentService.updateById(courseContent);
        CourseDemoEntity courseDemoEntity = courseContent.getCourseDemoEntity();
        QueryWrapper<CourseDemoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("content_id",courseContent.getCourseContentId());
         courseDemoService.remove(wrapper);
        if (courseDemoEntity!=null){
           courseDemoEntity.setContentId(courseContent.getCourseContentId());
           courseDemoService.save(courseDemoEntity);
       }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:coursecontent:delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Long[] courseContentIds){
		courseContentService.removeByIds(Arrays.asList(courseContentIds));
        QueryWrapper<CourseDemoEntity> wrapper = new QueryWrapper<>();
        wrapper.in("content_id",Arrays.asList(courseContentIds));
        courseDemoService.remove(wrapper);
        return R.ok();
    }

}
